package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Crawlerfilestandardetail;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("CrawlerfilestandardetailDAO")
@Transactional
public class CrawlerfilestandardetailDAOImpl extends AbstractJpaDao<Crawlerfilestandardetail>
		implements CrawlerfilestandardetailDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Crawlerfilestandardetail.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public CrawlerfilestandardetailDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Crawlerfilestandardetail findCrawlerfilestandardetailById(Integer id) throws DataAccessException {

		return findCrawlerfilestandardetailById(id, -1, -1);
	}

	@Transactional
	public Crawlerfilestandardetail findCrawlerfilestandardetailById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilestandardetailById", startResult, maxRows, id);
			return (Crawlerfilestandardetail) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfilestandardetail> findCrawlerfilestandardetailByName(String name) throws DataAccessException {

		return findCrawlerfilestandardetailByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandardetail> findCrawlerfilestandardetailByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestandardetailByName", startResult, maxRows, name);
		return new LinkedHashSet<Crawlerfilestandardetail>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfilestandardetail> findCrawlerfilestandardetailByNameContaining(String name)
			throws DataAccessException {

		return findCrawlerfilestandardetailByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandardetail> findCrawlerfilestandardetailByNameContaining(String name, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestandardetailByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Crawlerfilestandardetail>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfilestandardetail> findCrawlerfilestandardetailByDescription(String description)
			throws DataAccessException {

		return findCrawlerfilestandardetailByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandardetail> findCrawlerfilestandardetailByDescription(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestandardetailByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Crawlerfilestandardetail>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfilestandardetail> findCrawlerfilestandardetailByDescriptionContaining(String description)
			throws DataAccessException {

		return findCrawlerfilestandardetailByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandardetail> findCrawlerfilestandardetailByDescriptionContaining(String description,
			int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestandardetailByDescriptionContaining", startResult, maxRows,
				description);
		return new LinkedHashSet<Crawlerfilestandardetail>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfilestandardetail> findCrawlerfilestandardetailByNumber(Integer number)
			throws DataAccessException {

		return findCrawlerfilestandardetailByNumber(number, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandardetail> findCrawlerfilestandardetailByNumber(Integer number, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestandardetailByNumber", startResult, maxRows, number);
		return new LinkedHashSet<Crawlerfilestandardetail>(query.getResultList());
	}

	@Transactional
	public Crawlerfilestandardetail findCrawlerfilestandardetailByPrimaryKey(Integer id) throws DataAccessException {
		return findCrawlerfilestandardetailByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Crawlerfilestandardetail findCrawlerfilestandardetailByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilestandardetailByPrimaryKey", startResult, maxRows, id);
			return (Crawlerfilestandardetail) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfilestandardetail> findCrawlerfilestandardetailsByIds(Set<Integer> ids)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilestandardetailsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfilestandardetail> findAllCrawlerfilestandardetails() throws DataAccessException {

		return findAllCrawlerfilestandardetails(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandardetail> findAllCrawlerfilestandardetails(int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlerfilestandardetails", startResult, maxRows);
		return new LinkedHashSet<Crawlerfilestandardetail>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Crawlerfilestandardetail> findCrawlerfilestandardetailsByCrawlerfilestandard(
			Integer crawlerfilestandardId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlerfilestandardetailsByCrawlerfilestandard", start, max,
				crawlerfilestandardId);
		return new LinkedHashSet<Crawlerfilestandardetail>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Crawlerfilestandardetail> findCrawlerfilestandardetailsByCrawlerfilestandardcolumn(
			Integer crawlerfilestandardcolumnId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlerfilestandardetailsByCrawlerfilestandardcolumn", start, max,
				crawlerfilestandardcolumnId);
		return new LinkedHashSet<Crawlerfilestandardetail>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandardetail> findCrawlerfilestandardetailByCompositeProperities(String sql, int startResult,
			int maxRows) {
		return new LinkedHashSet<Crawlerfilestandardetail>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Crawlerfilestandardetail> findCrawlerfilestandardetailsByDetachedCriteria(CmiPagination page)
			throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Crawlerfilestandardetail.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Crawlerfilestandardetail> crawlerfilestandardetails = new HashSet(
				detachedCriteria.getExecutableCriteria(session).setFirstResult(page.getSearchBeginCursor())
						.setMaxResults(page.getPaginationSize()).list());

		return crawlerfilestandardetails;
	}

	@Transactional
	public Crawlerfilestandardetail findCrawlerfilestandardetailByPhysicalPrimaryKey(Integer crawlerfilestandardId,
			Integer crawlerfilestandardcolumnId, Integer number) throws DataAccessException {
		return findCrawlerfilestandardetailByPhysicalPrimaryKey(crawlerfilestandardId, crawlerfilestandardcolumnId,
				number, -1, -1);
	}

	@Transactional
	public Crawlerfilestandardetail findCrawlerfilestandardetailByPhysicalPrimaryKey(Integer crawlerfilestandardId,
			Integer crawlerfilestandardcolumnId, Integer number, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilestandardetailByPhyscialPrimaryKey", startResult, maxRows,
					crawlerfilestandardId, crawlerfilestandardcolumnId, number);
			return (Crawlerfilestandardetail) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Crawlerfilestandardetail entity) {
		return true;
	}
}
